﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace omaura_oled
{
    /// <summary>
    ///  Class to read and access the config file
    /// </summary>
    public class ReadConfigFile
    {
        #region "Attributes"
        //- 
        private OmauraOled OledInterface;

        //-- font
        public string FontType = "";
        public int FontTypeSize;
        public string FontTypeClock = "";
        public int FontTypeClockSize;

        //-- general
        public int OledIsEnable;
        public int OledBackGroundIsEnable;
        public string Language = "";

        //-- text/logo
        public int LogoIsEnable;
        public int TextSize;

        //-- theme
        public string PathToTheme = "";

        //-- weather
        public int WeatherTemperatureInCelcius;

        //-- messages
        public string StartMessage = "";
        public string EndMessage = "";

        //-- positions
        public int xIcon;
        public int yIcon;
        public int xText;
        public int yText;
        public int xClock;
        public int yClock;
        public int xTimer;
        public int yTimer;

        //-- display
        public int ProgressBarWithMarquer;
        public int ProgressBarWithBorder;
        public int TextDisplayRightAndLeft;
        public int SendDeathImage;

        //--- plugin
        public int PluginIsEnable;

        //- path to the file
        private string PathToConfig_ini = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\omaura\\omaura oled\\Config.ini";
        //--
        #endregion "Attributes"

        #region "Constructor"
        //--
        public ReadConfigFile(OmauraOled parentApp)
        {
            OledInterface = parentApp;

            ReadInfile();
        }
        //--
        #endregion "Constructor"

        #region "Methods"
        //- load the parameters form the file
        public void ReadInfile()
        {
            int iLine = 0;
            string firstPara;
            string line;

            StreamReader fileReader;

          //  try
            {

                fileReader = new StreamReader(PathToConfig_ini);

                using (fileReader)
                {
                    while (((line = fileReader.ReadLine()) != null) && iLine < 255)
                    {
                        iLine++;

                        firstPara = line[0].ToString();
                        for (int charI = 1; charI < 4; charI++)
                            firstPara += line[charI].ToString();

                        //- Parameters
                        #region "font"
                        //--
                        if (firstPara == "Ft  ")
                        {
                            int i = 8;
                            FontType = "";
                            while (line[i] != '|')
                            {
                                FontType += line[i];
                                i++;
                            }
                        }
                        if (firstPara == "Fts ")
                            FontTypeSize = Convert.ToInt32(Convert.ToString(line[7]) + Convert.ToString(line[8]));
                        if (firstPara == "FtT ")
                        {
                            int i = 8;
                            FontTypeClock = "";
                            while (line[i] != '|')
                            {
                                FontTypeClock += line[i];
                                i++;
                            }
                        }
                        if (firstPara == "FtTs")
                            FontTypeClockSize = Convert.ToInt32(Convert.ToString(line[7]) + Convert.ToString(line[8]));
                        //--
                        #endregion "font"

                        #region "general"
                        //--
                        if (firstPara == "OIE ")
                        {
                            OledIsEnable = Convert.ToInt32(Convert.ToString(line[7]));
                            if (OledIsEnable == 1)
                                OledInterface.radioOledActiveOn.IsChecked = true;
                            else
                                OledInterface.radioOledActiveOff.IsChecked = true;
                        }
                        if (firstPara == "OBIE")
                        {
                            OledBackGroundIsEnable = Convert.ToInt32(Convert.ToString(line[7]));
                            if (OledBackGroundIsEnable == 1)
                                OledInterface.radioBgOn.IsChecked = true;
                            else
                                OledInterface.radioBgOff.IsChecked = true;
                        }
                        if (firstPara == "LANG")
                        {
                            Language = Convert.ToString(line[8]) + Convert.ToString(line[9]);
                            string TempLanguage = ReadInFileWithParameter(OledInterface.InstallPath + ".\\Resources\\Lang\\Languages.txt", "|" + Language + "|");

                            for (int iLang = 0; iLang < OledInterface.comboBoxLanguage.Items.Count; iLang++)
                                if (OledInterface.comboBoxLanguage.Items.GetItemAt(iLang).ToString().Equals(TempLanguage))
                                    OledInterface.comboBoxLanguage.SelectedIndex = iLang;
                        }
                        //--
                        #endregion "general"

                        #region "text/logo"
                        //--
                        if (firstPara == "LOIE")
                        {
                            LogoIsEnable = Convert.ToInt32(Convert.ToString(line[7]));
                            if (LogoIsEnable == 1)
                                OledInterface.radioButtonToolsLogo.IsChecked = true;
                            else
                                OledInterface.radioButtonToolsMessages.IsChecked = true;
                        }
                        if (firstPara == "TS  ")
                        {
                            TextSize = Convert.ToInt32(Convert.ToString(line[7]));
                            if (TextSize == 0)
                                OledInterface.radioButtonToolsMessagesTextSmall.IsChecked = true;
                            else if (TextSize == 2)
                                OledInterface.radioButtonToolsMessagesTextBig.IsChecked = true;
                            else
                            {
                                TextSize = 1;
                                OledInterface.radioButtonToolsMessagesTextMedium.IsChecked = true;
                            }
                        }
                        //--
                        #endregion "text/logo"

                        #region "theme"
                        //--
                        if (firstPara == "PaTT")
                        {
                            int i = 8;
                            PathToTheme = "";
                            while (line[i] != '|')
                            {
                                PathToTheme += line[i];
                                i++;
                            }
                            OledInterface.iconsFolderPath = OledInterface.InstallPath + ".\\Resources\\Themes\\" + PathToTheme + "\\Img\\";

                            for (int iTheme = 0; iTheme < OledInterface.listThemes.Items.Count; iTheme++)
                                if (OledInterface.listThemes.Items.GetItemAt(iTheme).ToString().Equals(PathToTheme))
                                    OledInterface.listThemes.SelectedIndex = iTheme;

                        }
                        //--
                        #endregion "theme"

                        #region "weather"
                        //--
                        if (firstPara == "WTIC")
                        {
                            WeatherTemperatureInCelcius = Convert.ToInt32(Convert.ToString(line[7]));
                            if (WeatherTemperatureInCelcius == 1)
                                OledInterface.radioTemperatureCelcius.IsChecked = true;
                            else
                                OledInterface.radioTemperatureFarenheit.IsChecked = true;
                        }
                        //--
                        #endregion "weather"

                        #region "messages"
                        //--
                        if (firstPara == "SMT ")
                        {
                            int i = 8;
                            StartMessage = "";
                            while (line[i] != '|')
                            {
                                StartMessage += line[i];
                                i++;
                            }
                        }
                        if (firstPara == "EMT ")
                        {
                            int i = 8;
                            EndMessage = "";
                            while (line[i] != '|')
                            {
                                EndMessage += line[i];
                                i++;
                            }
                        }
                        //--
                        #endregion "messages"

                        #region "positions"
                        //--
                        if (firstPara == "xI  ")
                            xIcon = Convert.ToInt32(Convert.ToString(line[7]) + Convert.ToString(line[8]) + Convert.ToString(line[9]));
                        if (firstPara == "yI  ")
                            yIcon = Convert.ToInt32(Convert.ToString(line[7]) + Convert.ToString(line[8]) + Convert.ToString(line[9]));
                        if (firstPara == "xCl ")
                            xClock = Convert.ToInt32(Convert.ToString(line[7]) + Convert.ToString(line[8]) + Convert.ToString(line[9]));
                        if (firstPara == "yCl ")
                            yClock = Convert.ToInt32(Convert.ToString(line[7]) + Convert.ToString(line[8]) + Convert.ToString(line[9]));
                        if (firstPara == "xT  ")
                            xText = Convert.ToInt32(Convert.ToString(line[7]) + Convert.ToString(line[8]) + Convert.ToString(line[9]));
                        if (firstPara == "yT  ")
                            yText = Convert.ToInt32(Convert.ToString(line[7]) + Convert.ToString(line[8]) + Convert.ToString(line[9]));
                        if (firstPara == "xTim")
                            xTimer = Convert.ToInt32(Convert.ToString(line[7]) + Convert.ToString(line[8]) + Convert.ToString(line[9]));
                        if (firstPara == "yTim")
                            yTimer = Convert.ToInt32(Convert.ToString(line[7]) + Convert.ToString(line[8]) + Convert.ToString(line[9]));
                        //--
                        #endregion "positions"

                        #region "display"
                        //--
                        if (firstPara == "PBWM")
                            ProgressBarWithMarquer = Convert.ToInt32(Convert.ToString(line[7]));
                        if (firstPara == "PBWB")
                            ProgressBarWithBorder = Convert.ToInt32(Convert.ToString(line[7]));
                        if (firstPara == "TDRL")
                            TextDisplayRightAndLeft = Convert.ToInt32(Convert.ToString(line[7]));
                        if (firstPara == "SDim")
                            SendDeathImage = Convert.ToInt32(Convert.ToString(line[7]));
                        //--
                        #endregion "display"

                        #region "plugin"
                        //--
                        if (firstPara == "PisE")
                        {
                            PluginIsEnable = Convert.ToInt32(Convert.ToString(line[7]));
                            if (PluginIsEnable == 1)
                                OledInterface.radioPluginEnable.IsChecked = true;
                            else
                                OledInterface.radioPluginDisable.IsChecked = true;
                        }
                        //--
                        #endregion "plugin"
                    }
                }
            }
            //catch (Exception)
            //{
            //    System.Windows.Forms.MessageBox.Show("AAP");
            //}
        }

        //- return parameters from a file 
        public string ReadInFileWithParameter(string FileName, string Parameter)
        {
            int i = 0;
            string firstPara;
            string line;
            string parameterData = "";

            using(StreamReader fileReader = new StreamReader(FileName))
            {
                while (((line = fileReader.ReadLine()) != null) && i < 255)
                {
                    i++;

                    firstPara = line[0].ToString();
                    for (int charI = 1; charI < 4; charI++)
                        firstPara += line[charI].ToString();

                    if (firstPara.Equals(Parameter))
                    {
                        // Read the data
                        if (line[7] == '|')
                        {
                            int iSM = 8;

                            while (line[iSM] != '|')
                            {
                                parameterData += line[iSM];
                                iSM++;
                            }
                        }
                        else if (line.Length > 9 && line[7] != '|')
                        {
                            parameterData = line[7].ToString() + line[8].ToString() + line[9].ToString();
                        }
                        else if (line.Length == 9 && line[7] != '|')
                        {
                            parameterData = line[7].ToString() + line[8].ToString();
                        }
                        else
                        {
                            parameterData = line[7].ToString();
                        }
                    }
                }
            }
            // Return the data
            return (parameterData);
        }

        #region "Get/Set"
        //- get Fonts value (String)/(int)
        public string get_FontType() { return FontType; }
        public int get_FontTypeSize() { return FontTypeSize; }
        public string get_FontTypeClock() { return FontTypeClock; }
        public int get_FontTypeClockSize() { return FontTypeClockSize; }
        public int get_TextSize() { return TextSize; }
        
        //- get Icon position
        public int get_xIcon() { return xIcon; }
        public int get_yIcon() { return yIcon; }

        //- get Text position
        public int get_xText() { return xText; }
        public int get_yText() { return yText; }

        //- get Time position
        public int get_xClock() { return xClock; }
        public int get_yClock() { return yClock; }

        //- get Timer position
        public int get_xTimer() { return xTimer; }
        public int get_yTimer() { return yTimer; }

        //--
        #endregion "Get/Set"

        //--
        #endregion "Methods"
    }
}